home *** CD-ROM | disk | FTP | other *** search
- /*
- * pcc.c
- *
- * Practical Algorithms for Image Analysis
- *
- * Copyright (c) 1997, 1998, 1999 MLMSoftwareGroup, LLC
- */
-
- /* PCC: program performs primitives chain coding (PCC) of binarized
- * * and thinned image.
- * * usage: pcc inimg outfile [-I] [-L]
- * *
- */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <tiffimage.h> /* tiff file format info */
- #include <images.h> /* images information file */
- #include "pcc2.h" /* header file for PCC programs */
- extern void print_sos_lic ();
-
- unsigned char *fcCode; /* code storage */
- long nByteCode; /* no. bytes in storage */
-
- long input (int, char **, long *);
- long usage (short);
-
- main (argc, argv)
- int argc;
- char *argv[];
- {
- Image *imgI; /* input image structure */
- unsigned char **image; /* image array */
- long heightI, widthI; /* height and width of image */
- long nByteImg; /* no. bytes in image */
- long invertFlag; /* invert input image before processing */
- long x, y;
-
- if ((input (argc, argv, &invertFlag)) < 0)
- return (-1);
-
- /* open input file */
- imgI = ImageIn (argv[1]);
- image = imgI->img;
- heightI = ImageGetHeight (imgI);
- widthI = ImageGetWidth (imgI);
- printf ("image size is %dx%d\n", widthI, heightI);
- nByteImg = widthI * heightI;
-
- /* invert image for lines black=255, as expected in processing */
- if (invertFlag) {
- for (y = 0; y < heightI; y++)
- for (x = 0; x < widthI; x++)
- image[y][x] = 255 - image[y][x];
- }
-
- /* allocate storage for primitives chain code */
- if ((fcCode = (unsigned char *) malloc (nByteImg)) == NULL) {
- printf ("MALLOC: not enough memory -- sorry", 1);
- return (-1);
- }
- nByteCode = 0;
-
- /* construct tables of feature chain codes */
- pcccodes ();
-
- /* perform feature chain coding, and write out pcc file */
- printf ("coding being performed\n");
- pcccode (image, widthI, heightI);
-
- printf ("PCC is %3.2f%% of original image (%4.2f%% of binary)\n",
- (nByteCode * 100.0) / nByteImg, (nByteCode * 800.0) / nByteImg);
-
- /* write output PCC file */
- pccwrite (argv[2], fcCode, nByteCode, widthI, heightI);
-
- return (0);
- }
-
-
- /* USAGE: function gives instructions on usage of program
- * usage: usage (flag)
- * When flag is 1, the long message is given, 0 gives short.
- */
-
- long
- usage (flag)
- short flag; /* flag =1 for long message; =0 for short message */
- {
-
- /* print short usage message or long */
- printf ("USAGE: pcc inimg outfile [-I] [-L]\n");
- if (flag == 0)
- return (-1);
-
- printf ("\npcc produces Primitives Chain Code (PCC)\n");
- printf ("from input line image and writes this output\n");
- printf ("to a file containing PCC code.\n");
- printf ("NOTE: input image should be a line image\n\n");
- printf ("ARGUMENTS:\n");
- printf (" inimg: input image filename (TIF)\n");
- printf (" outfile: output file containing PCC (BINARY)\n\n");
- printf ("OPTIONS:\n");
- printf (" -I: invert input image before processing\n");
- printf (" -L: print Software License for this module\n");
-
- return (-1);
- }
-
-
- /* INPUT: function reads input parameters
- * usage: input (argc, argv)
- */
-
- #define USAGE_EXIT(VALUE) {usage (VALUE); return (-1);}
-
- long
- input (argc, argv, invertFlag)
- int argc;
- char *argv[];
- long *invertFlag; /* invert input image before processing */
- {
-
- long n;
-
- if (argc == 1)
- USAGE_EXIT (1);
- if (argc == 2)
- USAGE_EXIT (0);
-
- *invertFlag = 0;
-
- for (n = 3; n < argc; n++) {
- if (strcmp (argv[n], "-I") == 0)
- *invertFlag = 1;
- else if (strcmp (argv[n], "-L") == 0) {
- print_sos_lic ();
- exit (0);
- }
- else
- USAGE_EXIT (0);
- }
-
- return (0);
- }
-